package com.example.fx.order

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.CreateOrderParamEntity
import com.example.base.entity.PayParamEntity
import com.example.base.mvvm.BaseActivity
import com.example.fx.R
import com.example.fx.adapter.OrderAdapter
import com.example.fx.databinding.ActivityOrderBinding

@Route(path = "/app/OrderActivity")
class OrderActivity : BaseActivity<OrderViewModel,ActivityOrderBinding>() {
    lateinit var orderAdapter: OrderAdapter
    @Autowired
    @JvmField
    var preOrderNo = ""

    var payType=""//支付方式
    override fun getLayout(): Int  = R.layout.activity_order

    override fun getViewModel(): Class<OrderViewModel> = OrderViewModel::class.java

    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.getPreOrder(preOrderNo)

        orderAdapter = OrderAdapter()
        binding.rvOrder.adapter = orderAdapter
        binding.rvOrder.layoutManager = LinearLayoutManager(this)

        binding.rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.zifubao->{
                    payType="weixin"
                }
                R.id.yue->{
                    payType = "yue"
                }
            }
        }
        //点击支付，完成结算功能
        binding.pay.setOnClickListener {
            val createOrderParamEntity  = CreateOrderParamEntity()
            createOrderParamEntity.payType = payType//支付类型
            createOrderParamEntity.payChannel = "weixinh5"//支付渠道
            createOrderParamEntity.preOrderNo = preOrderNo//预下单编号
            createOrderParamEntity.shippingType = 1//快递方式
            createOrderParamEntity.addressId = 5//地址编号
            createOrderParamEntity.mark = ""//备注
            viewModel.createOrder(createOrderParamEntity)
        }
    }

    override fun initData() {
        viewModel.getPreSuccess.observe(this){
            binding.tvAddress.text = "${it.data.orderInfoVo.province}${it.data.orderInfoVo.city}${it.data.orderInfoVo.district}${it.data.orderInfoVo.detail}"//详细地址
            binding.tvTotal.text = "合计：${it.data.orderInfoVo.proTotalFee}"
            //展示订单详情
            orderAdapter.data.clear()
            orderAdapter.data.addAll(it.data.orderInfoVo.orderDetailList)
            orderAdapter.notifyDataSetChanged()
        }
        viewModel.createSuccess.observe(this){
            if(it.code==200){
                //支付
                val payParamEntity = PayParamEntity()
                payParamEntity.orderNo = it.data.orderNo
                payParamEntity.payType = payType
                payParamEntity.payChannel = "weixinh5"
                viewModel.pay(payParamEntity)
            }
        }
        viewModel.paySuccess.observe(this){
            ToastUtils.showLong(it.message)
            //可跳转到支付结果界面

        }
    }

}